package histogram;

import java.util.ArrayList;

import org.encog.neural.data.NeuralData;
import org.encog.neural.data.basic.BasicNeuralData;
import org.encog.neural.networks.BasicNetwork;
import org.encog.persist.EncogPersistedCollection;

public class NeiroWork extends AbstractNeiro {

    BasicNetwork network = null;

    public void loadNetwork(String FILENAME) {
        GlobLogger.log("Loading network...");

        final EncogPersistedCollection encogCollection = new EncogPersistedCollection(
                FILENAME);
        network = (BasicNetwork) encogCollection.find(NET_NAME);
    }
    ArrayList<Integer> answers;
    int size = 0;
    private double mas[][];
    private ArrayList<double[]> maslist;

    public ArrayList<Integer> workNetwork(ArrayList<double[]> input) {
        maslist = input;
        size = input.size();
        return  solve();
        
    }

    public ArrayList<Integer> workNetwork(double[][] input) {
        mas = input;
        size = input.length;
        return solve();
    }

    private ArrayList<Integer> solve(){

        GlobLogger.log("Start work network.");
        answers = new ArrayList<Integer>();

        for (int i = 0; i < size; i++) {

            double[] inputTmp = getNext(i);
            final NeuralData outputTmp = network.compute(new BasicNeuralData(
                    inputTmp));

            double max = 0;
            int actual = 0;
            for (int j = 0; j < outputTmp.size(); j++) {
                if (outputTmp.getData(j) > max) {
                    max = outputTmp.getData(j);
                    actual = j;
                }
            }
            answers.add(actual);

        }

        return answers;
    }
    
    private double[] getNext(int i) {
        if (maslist != null){
            return maslist.get(i);
        }
        if (mas != null){
            return mas[i];
        }
        return  null;
    }
}